//
//  BLEBaseAction.swift
//  Clock
//
//  Created by 方强 on 16/7/2.
//  Copyright © 2016年 MT. All rights reserved.
//

import UIKit
import CoreBluetooth

public class BLEBaseAction {
    private var statues = BLEBaseAction_ActionStatues.WAITTING;
    private var actiontype = BLEBaseAction_ActionType.UNKNOW;
    var option:BLEBaseAction_ActionOption!;
    
    public init(type:BLEBaseAction_ActionType!, option:BLEBaseAction_ActionOption!) {
        self.actiontype = type;
        self.option = option;
    }
    
    public func getStatues() -> BLEBaseAction_ActionStatues{
        return statues;
    }
    
    public func setStatues(statues:BLEBaseAction_ActionStatues!) {
        self.statues = statues;
    }
    
    public func getActiontype() -> BLEBaseAction_ActionType{
        return actiontype;
    }
    
    public func setActiontype(actiontype:BLEBaseAction_ActionType) {
        self.actiontype = actiontype;
    }
    
    public func getOption() -> BLEBaseAction_ActionOption{
        return option;
    }
    
    public func setActionOption(option: BLEBaseAction_ActionOption) {
        self.option = option;
    }
    
    // 回调
    public var onStart:((BLEBaseAction)->Void)?;

    public var onSuccess:((BLEBaseAction)->Void)?;
    
    public var onFail:((BLEBaseAction, ErroCode)->Void)?; // 发送失败

    public var onCancel:((BLEBaseAction)->Void)?;  // 被取消

}

// 设备类型
public enum BLEBaseAction_ActionType{
    case UNKNOW
    case READCHACT // 读特征值
    case WRITECHACT // 写特征值
    case READRSSI // 读取RSSI
    case WRITEDESCRIPTOR // 写Descriptor
    case READDESCRIPTOR // 读Descriptor
    
    case DISCOVERSERVICE  // 发现服务
    case DISCOVERVHARACTS // 发现特征值
    case SETNOTIFY  // 设置可通知属性
    case WRITECHACTWITHACK // 包括等待回复
    
    case SENDBIGDATAS  // 发送大数据
}

// 设备类型
public enum BLEBaseAction_ActionStatues{
    case WAITTING // 等待运行
    case RUNING // 正在运行
    case DONE // 运行完毕
    case CANCEL // 被取消
}

public class BLEBaseAction_ActionOption: NSObject{
    private var timeout:Int = 0;
    private var perlenght:Int = 20;
    private var waittime:Int = 20;  // 如果是无响应写数据，则等待20ms
    
    public init(timeout:Int) {
        super.init();
        self.timeout = timeout;
    }
    
    public func getTimeout() -> Double{
        return Double(timeout)/1000;
    }
    
    public func setTimeout(timeout:Int){
        self.timeout = timeout;
    }
    
    public func getPerlenght() -> Int{
        return perlenght;
    }
    
    public func setPerlenght(perlenght:Int) {
        self.perlenght = perlenght;
    }
    
    public func getWaittime() -> Double{
        return Double(waittime)/1000;
    }
    
    public func setWaittime(waittime:Int) {
        self.waittime = waittime;
    }
}
